package com.ganxiang.ximenhong.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ganxiang.ximenhong.R;
import com.ganxiang.ximenhong.base.BaseActivity;
import com.ganxiang.ximenhong.fragment.ClassificationFragment;
import com.ganxiang.ximenhong.fragment.HomePageFragment;
import com.ganxiang.ximenhong.fragment.MeFragment;
import com.ganxiang.ximenhong.fragment.ShoppingCarFragment;
import com.ganxiang.ximenhong.widght.TabBarView;

/**
 * Created by csp on 2016/9/13 0013.
 */
public class MainActivity extends BaseActivity{

    private TabBarView mTabBarView;
    private HomePageFragment mHomePageFragment;
    private ClassificationFragment mClassificationFragment;
    private ShoppingCarFragment mShoppingCarFragment;
    private MeFragment mMeFragment;
    private FragmentManager mFragmentManager;
    private FragmentTransaction mFragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initTabBarView();
    }

    private void initTabBarView() {
        mFragmentManager = getSupportFragmentManager();
        mTabBarView = (TabBarView) findViewById(R.id.tabbarview_bottom);
        final String[] texts = getResources().getStringArray(R.array.main_tab);
        final int[] imageSelect = {R.drawable.ico_sy,R.drawable.ico_fl_on,R.drawable.icon_gwc_on,R.drawable.ico_wd_on};
        final int[] imageNor = {R.drawable.ico_sy_off,R.drawable.ico_fl,R.drawable.ico_gwc,R.drawable.ico_wd};
        mTabBarView.setAdapter(new TabBarView.TabBarAdapter() {
            @Override
            public int getCount() {
                return texts.length;
            }

            @Override
            public View getOnView(int position, View convertView) {
                if(convertView == null){
                    convertView = View.inflate(MainActivity.this,R.layout.item_main_tab,null);
                }
                ((TextView)convertView.findViewById(R.id.tv)).setText(texts[position]);
                ((TextView)convertView.findViewById(R.id.tv)).setTextColor(Color.parseColor("#F23030"));
                ((ImageView)convertView.findViewById(R.id.iv)).setImageResource(imageSelect[position]);
                return convertView;
            }

            @Override
            public View getOffView(int position, View convertView) {
                if(convertView == null){
                    convertView = View.inflate(MainActivity.this,R.layout.item_main_tab,null);
                }
                ((TextView)convertView.findViewById(R.id.tv)).setText(texts[position]);
                ((TextView)convertView.findViewById(R.id.tv)).setTextColor(Color.parseColor("#8D8D99"));
                ((ImageView)convertView.findViewById(R.id.iv)).setImageResource(imageNor[position]);

                return convertView;
            }

            @Override
            public void onFocus(View v, boolean hasFocus) {

            }
        });
        mTabBarView.setOnItemSelectedListener(new TabBarView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int position) {
                mFragmentTransaction = mFragmentManager.beginTransaction();
                dismissFragment();
                switch (position){
                    case 0:
                        if(mHomePageFragment == null){
                            mHomePageFragment = new HomePageFragment();
                            mFragmentTransaction.add(R.id.fragment_contrain,mHomePageFragment);
                            mFragmentTransaction.show(mHomePageFragment);
                        }else{
                            mFragmentTransaction.show(mHomePageFragment);
                        }
                        break;

                    case 1:
                        if(mClassificationFragment == null){
                            mClassificationFragment = new ClassificationFragment();
                            mFragmentTransaction.add(R.id.fragment_contrain,mClassificationFragment);
                            mFragmentTransaction.show(mClassificationFragment);
                        }else{
                            mFragmentTransaction.show(mClassificationFragment);
                        }
                        break;

                    case 2:
                        if(mShoppingCarFragment == null){
                            mShoppingCarFragment = new ShoppingCarFragment();
                            mFragmentTransaction.add(R.id.fragment_contrain,mShoppingCarFragment);
                            mFragmentTransaction.show(mShoppingCarFragment);
                        }else{
                            mFragmentTransaction.show(mShoppingCarFragment);
                        }
                        break;

                    case 3:
                        if(mMeFragment == null){
                            mMeFragment = new MeFragment();
                            mFragmentTransaction.add(R.id.fragment_contrain,mMeFragment);
                            mFragmentTransaction.show(mMeFragment);
                        }else{
                            mFragmentTransaction.show(mMeFragment);
                        }
                        break;
                    default:
                        break;
                }
                mFragmentTransaction.commitAllowingStateLoss();
            }
        });
        mTabBarView.selectItem(0);
    }

    private void dismissFragment(){
        if(mHomePageFragment != null ){
            mFragmentTransaction.hide(mHomePageFragment);
        }
        if(mClassificationFragment != null){
            mFragmentTransaction.hide(mClassificationFragment);
        }
        if(mShoppingCarFragment != null){
            mFragmentTransaction.hide(mShoppingCarFragment);
        }
        if(mMeFragment != null){
            mFragmentTransaction.hide(mMeFragment);
        }
    }
}
